package speed2010.reformulacao.query;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;

import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.query.QueryResults;
/**
 * Executes the sparql query in the owl file
 * 
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class QueryProtege {

	/**
	 * Execute a sparql query into an ontology
	 * 
	 * @param query
	 *            - The SPARQL query to be executed
	 * @param fileArray
	 *            - The byte[] of the target ontology
	 * @return - A {@link QueryResults} instance
	 * @throws Exception
	 *             - If any problem occurs during the query processing
	 */
	public static QueryResults execSearch(String query, byte[] fileArray) throws Exception {
		ByteArrayInputStream stream = new ByteArrayInputStream(fileArray);

		OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromInputStream(stream);
		QueryResults results = owlModel.executeSPARQLQuery(query);

		return results;
	}

	public static QueryResults execSearch(String path, String query) throws Exception {
		FileInputStream stream = new FileInputStream(new File(path));
		OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromInputStream(stream);
		QueryResults results = owlModel.executeSPARQLQuery(query);

		return results;
	}
}
